package com.yc.jpyy.driverpoint.controller;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.common.utils.manydatasource.HandlerDataSource;
import com.yc.jpyy.base.bean.RestResponse;
import com.yc.jpyy.base.controller.BaseController;
import com.yc.jpyy.base.util.PageBean;
import com.yc.jpyy.driver.service.DriverService;
import com.yc.jpyy.driverpoint.service.DriverPointService;
import com.yc.jpyy.drivertrain.service.DriverTrainService;
import com.yc.jpyy.utils.Tools;




@Controller
@RequestMapping(value = "/driverPoint")
@Scope("prototype")
public class DirverPointController extends BaseController {
	
	@Autowired
	private DriverPointService driverPointService;
    private static Logger logger = LoggerFactory.getLogger(DirverPointController.class);
	
	@SuppressWarnings("finally")
	@RequestMapping(value = "/driverPointAdd", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json; charset=utf-8")
	public @ResponseBody String driverPointAdd(HttpServletRequest request) throws UnsupportedEncodingException {
		
		RestResponse response = new RestResponse(Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
		String info = request.getParameter("info");
		try{
			System.out.println(info);
			Map<String,Object> paramsMap = Tools.JsonToMap(info);
			System.out.println(paramsMap);
			String dbcode = paramsMap.get("dbcode").toString();
			HandlerDataSource.setDataSource(dbcode);
			driverPointService.addDriverPointServer(paramsMap);
			response.setReturnMsg(1, "操作成功");
			return response.toJSON();
			
		} catch (Exception e) {
			logger.error("新增司机分数异常" + e.toString());
			// 异常信息
			response.setReturnCode(0);
			response.setReturnMsg("操作失败");
			response.setReturnException(e.toString());
			e.printStackTrace();
		}
		
		finally {
			HandlerDataSource.clear();
			return response.toJSON();
		}
		
	}
	
	@SuppressWarnings("finally")
	@RequestMapping(value = "/delDriverPoint", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json; charset=utf-8")
	public @ResponseBody String delDriverPoint(HttpServletRequest request) throws UnsupportedEncodingException {
		
		RestResponse response = new RestResponse(Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
		Integer id =  Integer.parseInt(request.getParameter("id"));
		
		try{
			
			String info = request.getParameter("info");
			System.out.println("info"+info);
			Map<String,Object> paramsMap = Tools.JsonToMap(info);
			String dbcode = paramsMap.get("dbcode").toString();
			HandlerDataSource.setDataSource(dbcode);
			int ret = driverPointService.delDriverPointServer(paramsMap);
			if(ret==1) {
				response.setReturnMsg(1, "删除成功");
				return response.toJSON();
			}
			else{
				
			    response.setReturnMsg(0, "删除失败");
			    return response.toJSON();
			}
			
		} catch (Exception e) {
			logger.info("删除公司异常" + e.toString());
			// 异常信息
			response.setReturnException(e.toString());
			response.setReturnCode(0);
			response.setReturnMsg("操作失败");
			e.printStackTrace();
		}
		
		finally {
			HandlerDataSource.clear();
			return response.toJSON();
		}
		
	}
	
	@SuppressWarnings("finally")
	@RequestMapping(value = "/finddriverPointByID", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json; charset=utf-8")
	public @ResponseBody String finddriverPointByID(HttpServletRequest request) throws UnsupportedEncodingException {

		RestResponse response = new RestResponse(Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
		try {
			String info = request.getParameter("info");
			System.out.println("info"+info);
			Map<String,Object> paramsMap = Tools.JsonToMap(info);
			String dbcode = paramsMap.get("dbcode").toString();
			HandlerDataSource.setDataSource(dbcode);
			Map<String, Object> val = driverPointService.findDriverPointByIDServer(paramsMap);
            response.setReturnMsg(1, "查询单条数据成功");
			response.setListData(val);
		    return response.toJSON();
			

		} catch (Exception e) {
            logger.info("查询单条数据异常" + e.toString());
			// 异常信息
            response.setReturnException(e.toString());
			response.setReturnCode(0);
			response.setReturnMsg("查询单条数据异常");

		} finally {
			HandlerDataSource.clear();
			return response.toJSON();
		}
	}
	
	
	@SuppressWarnings("finally")
	@RequestMapping(value = "/updateDriverPoint", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json; charset=utf-8")
	public @ResponseBody String updateDriverPoint(HttpServletRequest request) throws UnsupportedEncodingException {
		
		RestResponse response = new RestResponse(Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
		String driverInfo = request.getParameter("driverInfo");
		try{
			System.out.println(driverInfo);
			Map<String,Object> paramsMap = Tools.JsonToMap(driverInfo);
			String dbcode = paramsMap.get("dbcode").toString();
			HandlerDataSource.setDataSource(dbcode);
			driverPointService.updateDriverPointServer(paramsMap);
			response.setReturnMsg(1, "操作成功");
			return response.toJSON();
			
		} catch (Exception e) {
			logger.info("异常" + e.toString());
			// 异常信息
			response.setReturnCode(0);
			response.setReturnMsg("操作失败");
			response.setReturnException(e.toString());
			e.printStackTrace();
		}
		
		finally {
			HandlerDataSource.clear();
			return response.toJSON();
		}
		
	}
	
	/**
	 * 
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/findDriverPoint", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json; charset=utf-8")
	public @ResponseBody String findDriverPoint(HttpServletRequest request) throws UnsupportedEncodingException {

		RestResponse response = new RestResponse(Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
		request.setCharacterEncoding("UTF-8");
		try {
			String info = request.getParameter("info");
			Map<String,Object> paramsMap = Tools.JsonToMap(info);
			String dbcode = paramsMap.get("dbcode").toString();
			HandlerDataSource.setDataSource(dbcode);
            PageBean pageBean = driverPointService.findDriverPointServer(paramsMap);
            response.setReturnMsg(1, "操作成功");
            response.setTotalCount(pageBean.getAllRow());
            response.setTotalPage(pageBean.getTotalPage());    
			response.setListData(pageBean.getList());
			return response.toJSON();
            
		} catch (Exception e) {
			logger.info("queryTiket" + e.toString());
			response.setReturnCode(1);
			response.setReturnMsg("查询失败！");
		} finally {
			HandlerDataSource.clear();
			return response.toJSON();
		}
	}
	
	 //物理删除
		@SuppressWarnings("finally")
		@RequestMapping(value = "/delDriverAddPoint", method = { RequestMethod.GET,
				RequestMethod.POST }, produces = "application/json; charset=utf-8")
		public @ResponseBody String delDriverAddPoint(HttpServletRequest request) throws UnsupportedEncodingException {
			
			RestResponse response = new RestResponse(Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
			try{
				String info = request.getParameter("info");
				System.out.println("info"+info);
				Map<String,Object> paramsMap = Tools.JsonToMap(info);
				System.out.println("paramsMap"+paramsMap);
				String ids = paramsMap.get("ids").toString();
				String[] idarray = ids.split(",");
				paramsMap.put("idarray", idarray);
				String dbcode = paramsMap.get("dbcode").toString();
			    //设置数据源
				HandlerDataSource.setDataSource(dbcode);
				int ret = driverPointService.delDriverAddPointServer(paramsMap);
				if(ret>0) {
					response.setReturnMsg(1, "删除成功");
					return response.toJSON();
				}
				else{
				    response.setReturnMsg(0, "删除失败");
				    return response.toJSON();
				}
				
			} catch (Exception e) {
				logger.error("删除司机异常" + e.toString());
				// 异常信息
				response.setReturnCode(0);
				response.setReturnMsg("操作失败");
				response.setReturnException(e.toString());
				e.printStackTrace();
			}
			
			finally {
				HandlerDataSource.clear();
				return response.toJSON();
			}
			
		}
}
